package com.kld.yqyj.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.kld.common.utils.StringUtils;
import com.kld.common.core.page.TableDataInfo;
import com.kld.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.kld.yqyj.domain.bo.TGwDBo;
import com.kld.yqyj.domain.vo.TGwDVo;
import com.kld.yqyj.domain.TGwD;
import com.kld.yqyj.mapper.TGwDMapper;
import com.kld.yqyj.service.ITGwDService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 采气井生产日报Service业务层处理
 *
 * @author bw
 * @date 2024-05-15
 */
@RequiredArgsConstructor
@Service
public class TGwDServiceImpl implements ITGwDService {

    private final TGwDMapper baseMapper;

    /**
     * 查询采气井生产日报
     *
     * @param wellId 采气井生产日报主键
     * @return 采气井生产日报
     */
    @Override
    public TGwDVo queryById(String wellId){
        return baseMapper.selectVoById(wellId);
    }

    /**
     * 查询采气井生产日报列表
     *
     * @param bo 采气井生产日报
     * @return 采气井生产日报
     */
    @Override
    public TableDataInfo<TGwDVo> queryPageList(TGwDBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TGwD> lqw = buildQueryWrapper(bo);
        Page<TGwDVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询采气井生产日报列表
     *
     * @param bo 采气井生产日报
     * @return 采气井生产日报
     */
    @Override
    public List<TGwDVo> queryList(TGwDBo bo) {
        LambdaQueryWrapper<TGwD> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TGwD> buildQueryWrapper(TGwDBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TGwD> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getWellName()), TGwD::getWellName, bo.getWellName());
        lqw.eq(StringUtils.isNotBlank(bo.getCW()), TGwD::getCW, bo.getCW());
        lqw.eq(bo.getProdTime() != null, TGwD::getProdTime, bo.getProdTime());
        lqw.eq(bo.getTubingPre() != null, TGwD::getTubingPre, bo.getTubingPre());
        lqw.eq(bo.getCasingPre() != null, TGwD::getCasingPre, bo.getCasingPre());
        lqw.eq(bo.getDG() != null, TGwD::getDG, bo.getDG());
        lqw.eq(bo.getDO() != null, TGwD::getDO, bo.getDO());
        lqw.eq(bo.getDW() != null, TGwD::getDW, bo.getDW());
        lqw.eq(bo.getGP() != null, TGwD::getGP, bo.getGP());
        lqw.eq(bo.getWP() != null, TGwD::getWP, bo.getWP());
        return lqw;
    }

    /**
     * 新增采气井生产日报
     *
     * @param bo 采气井生产日报
     * @return 结果
     */
    @Override
    public Boolean insertByBo(TGwDBo bo) {
        TGwD add = BeanUtil.toBean(bo, TGwD.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setWellId(add.getWellId());
        }
        return flag;
    }

    /**
     * 修改采气井生产日报
     *
     * @param bo 采气井生产日报
     * @return 结果
     */
    @Override
    public Boolean updateByBo(TGwDBo bo) {
        TGwD update = BeanUtil.toBean(bo, TGwD.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(TGwD entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除采气井生产日报
     *
     * @param wellIds 需要删除的采气井生产日报主键
     * @return 结果
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
